﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class NeedMaterialHeaderEditFromSalesOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!this.IsPostBack)
            {
                if (Request.QueryString["SalesOrderID"] != null)
                {
                    if (Request.QueryString["SalesOrderID"].ToString() != null)
                    {
                        //用一个隐藏Label保存传递过来的ID
                        this.lblSalesOrderID.Text = Request.QueryString["SalesOrderID"].ToString();
                    }
                    else
                    {
                        mk.Alert("参数错误！");
                    }
                }
                else
                {
                    mk.Alert("参数错误！");
                }
                //设置经办人信息
                Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();


                this.lblManagerTrueName.Text = currentUser.TrueName;
                this.lblManagerUserName.Text = currentUser.UserName;
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                this.lblRootDepartmentID.Text = bllDept.Where(h => h.ClassID == (int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                //给日期输入框添加只读属性，不能在属性里直接设置，不然会取不到值
              //  this.txtDateOrder.Attributes["readonly"] = "true";
                this.txtDateOrder.Date = System.DateTime.Now;
                //显示订单概况
                this.ShowSalesOrderHeaderInfo();
            }
        }
        private void ShowSalesOrderHeaderInfo()
        {
            //实例化销售订单业务逻辑类
            var bllHeader =
               mk.l.Sales_SalesOrderHeader.ToList();
            //根据销售订单编号得到销售订单类
            Sales_SalesOrderHeader mHeader =
                bllHeader.Single(h => h.SalesOrderID == (int.Parse(this.lblSalesOrderID.Text)));
            if (mHeader != null)
            {
                //判断是否是本店的订单
                if (int.Parse(this.lblRootDepartmentID.Text) ==
                     mHeader.RootDepartmentID)
                {
                    //判断流程是否已经完成，如果流程已完成，
                    //则不显示添加订货单界面，否则允许用户添加材料需求
                    if (mHeader.StatusAll == "2")
                    {
                        this.pnlAdd.Visible = false;
                        this.pnlAdd.Enabled = false;
                    }
                    //在用户界面上显示订单编号和订单描述
                    this.lblSalesOrderNumber.Text = mHeader.SalesOrderNumber;
                    this.lblNotesOrder.Text = mHeader.NotesOrder;
                    var bllUser =
                        mk.l.Accounts_Users.ToList();
                    Accounts_Users mUser =
                              bllUser.Single(h => h.UserName == (mHeader.Designer));
                    //显示用户名称
                    this.lblDesigner.Text = mUser.TrueName;
                    this.BindNeedMaterialHeader();//绑定用料列表Grid控件
                }
                else
                {
                    mk.Alert("只能操作本店的订单！");
                }

            }
            else
            {
                mk.Alert("参数错误！",
                    "SalesOrderListForPurchaseOrderHeader.aspx");
            }
        }
        private void BindNeedMaterialHeader()
        {
            //实例化用料单业务逻辑类
            var bllHeader =
                mk.l.Sales_NeedMaterialHeader.ToList();


            //调用GetList方法获取指定销售定单的用料单列表
            var ds = bllHeader.Where(h => h.SalesOrderID.ToString() ==
                   this.lblSalesOrderID.Text);
            //执行数据绑定
            this.myGridView.DataSource = ds;
            this.myGridView.DataBind();
        }
        protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblManagerName = (Label)(e.Row.FindControl("lblManagerName"));
                var bllUser = mk.l.Accounts_Users.ToList();
                Accounts_Users mUser = bllUser.Single(h => h.UserName == (this.myGridView.DataKeys[e.Row.RowIndex].Values["Manager"].ToString()));
                lblManagerName.Text = mUser.TrueName;

                //显示提交状态
                Label lblStatusSubmit = (Label)(e.Row.FindControl("lblStatusSubmit"));
                string strStatusSubmit = this.myGridView.DataKeys[e.Row.RowIndex].Values["StatusSubmit"].ToString();
                switch (strStatusSubmit)
                {
                    case "1":
                        lblStatusSubmit.Text = "初始添加";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblStatusSubmit.Text = "已提交";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblStatusSubmit.Text = "未知";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
                        break;
                }

                //显示订货状态
                Label lblStatusPurchaseMaterial = (Label)(e.Row.FindControl("lblStatusPurchaseMaterial"));
                string strStatusPurchaseMaterial = this.myGridView.DataKeys[e.Row.RowIndex].Values["StatusPurchaseMaterial"].ToString();
                switch (strStatusPurchaseMaterial)
                {
                    case "1":
                        lblStatusPurchaseMaterial.Text = "未订货";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblStatusPurchaseMaterial.Text = "已订货";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblStatusPurchaseMaterial.Text = "未知";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
                        break;
                }
            }
        }

        protected void myGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            this.lblMsg2.Text = string.Empty;

            try
            {
                mk.l.Sales_NeedMaterialHeader.DeleteOnSubmit(mk.l.Sales_NeedMaterialHeader.ToList().Single(h => h.NeedMaterialID == int.Parse(this.myGridView.DataKeys[e.RowIndex].Values["NeedMaterialID"].ToString())));
                mk.l.SubmitChanges(); //判断订单的提交状态
                var bllOrderHeader = mk.l.Sales_SalesOrderHeader.ToList();
                Sales_SalesOrderHeader mOrderHeader = bllOrderHeader.Single(h => h.SalesOrderID == (int.Parse(this.lblSalesOrderID.Text)));
                mOrderHeader.StatusSubmit = mk.GetStatusSubmit(mOrderHeader.SalesOrderID);
                mOrderHeader.StatusPurchaseMaterial = mk.GetStatusPurchaseMaterial(mOrderHeader.SalesOrderID);
                mk.l.SubmitChanges();
                this.BindNeedMaterialHeader();
                //提交事务
                //ContextUtil.SetComplete();
            }
            catch
            {
                //撤销事务
               // ContextUtil.SetAbort();
                this.lblMsg2.Text = "删除失败！";
            }
        }
        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (this.myGridView.Rows.Count > 0)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
        protected void btnAddOrder_Click(object sender, EventArgs e)
        {

            this.lblMsg2.Text = string.Empty;
            //实例化用料单业务逻辑处理类
            // var bll =
            //      mk.l.Sales_NeedMaterialHeader.ToList();
            //实例化用料单实体类
            Sales_NeedMaterialHeader m =
                 new Sales_NeedMaterialHeader();
            //从用户界面中获取用料单用户界面输入
            m.DateOrder = Convert.ToDateTime(this.txtDateOrder.Date);
            m.Manager = this.lblManagerUserName.Text;
            m.ModifiedDate = System.DateTime.Now;
            m.RootDepartmentID = int.Parse(this.lblRootDepartmentID.Text);
            m.SalesOrderID = int.Parse(this.lblSalesOrderID.Text);
            //初始化用料单状态
            m.StatusPurchaseMaterial = "1";
            m.StatusSubmit = "1";
            m.StatusStockOut = "1";
            try
            {
                mk.l.Sales_NeedMaterialHeader.InsertOnSubmit(m);   //向数据库中添加用料单
                //实例化销售订单
                var bllOrderHeader =
                    mk.l.Sales_SalesOrderHeader.ToList();
                Sales_SalesOrderHeader mOrderHeader =
                         bllOrderHeader.Single(h => h.SalesOrderID == (int.Parse(this.lblSalesOrderID.Text)));
                //获取当前销售订单的提交状态
                mOrderHeader.StatusSubmit =
               mk.GetStatusSubmit(mOrderHeader.SalesOrderID);
                //获取材料订货单状态
                mOrderHeader.StatusPurchaseMaterial =
                   mk.GetStatusPurchaseMaterial(mOrderHeader.SalesOrderID);
                //更新销售订单
                mk.l.SubmitChanges();
                this.lblMsg2.Text = "添加成功！请确认用料单明细";
                this.BindNeedMaterialHeader();   //绑定订货单概况列表                    
               // ContextUtil.SetComplete();       //提交事务
            }
            catch (Exception ex)
            {
                // ContextUtil.SetAbort();          //撤销事务          
                this.lblMsg2.Text = "添加错误！错误信息：" + ex.Message;
            }

        }
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {
                mk.Alert("修改用料单成功！将返回到订单列表", "SalesOrderList.aspx");
            }
        }
    }
}